package com.alibaba.org.controller;


import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.org.dto.ShopDto;
import com.alibaba.basic.util.ExcelUtil;
import com.alibaba.basic.util.PageList;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.domain.ShopAuditLog;
import com.alibaba.org.query.ShopQuery;
import com.alibaba.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单条数据",notes = "查询单挑数据操作")
    public Shop findOne(@PathVariable("id") Long id ){
        return shopService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有数据",notes = "查询所有操作")
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询",notes = "分页查询操作")
    public PageList<Shop> pageQuery(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除",notes = "部门删除操作")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception a){
            a.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult save(@RequestBody Shop shop){
        try {
            if(shop.getId()!=null){
                shopService.update(shop);
            } else {
                shopService.add(shop);
            }
            return new AjaxResult();
        } catch (Exception a){
            a.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "部门批量删除",notes = "部门批量删除操作")
    public AjaxResult atchDelete(@RequestBody Long[] id){
        try {
            shopService.atchDelete(id);
            return new AjaxResult();
        } catch (Exception a){
            a.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：店铺入驻
     * @param
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败");
        }
    }

    /**
     * 接口:店铺激活,然后跳转到登录页面
     * @param id
     * @param resp
     */
    @PostMapping("/active{id}")
    @ApiOperation(value = "店铺激活成功")
    public void active(@PathVariable("id") Long id ,HttpServletResponse resp){
        try {
            shopService.active(id);
            resp.sendRedirect("http://localhost:8081/login?id="+id);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 接口：店铺审核驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"驳回失败");
        }
    }



    @GetMapping("/export")
    public void export( HttpServletResponse response){
            List<Shop> shops = shopService.findAll();
            //shops：导出的数据
            //null:标题 - 一般无
            //"店铺信息" - sheet页的名称
            //Shop.class 导出的类型
            //"shop.xlsx - 导出文件名
            //response - 导出时的响应对象
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){

        //file - 要导入的文件 - 前端上传的文件
        // 0, 标题行数 - 没有
        // 1, 头函数 = 字段名
        // Shop.class = 导入时映射的对象
        ExcelUtil.importExcel(file,0,1,Shop.class);
    }


    @GetMapping("/echarts")
    public List<ShopDto> getShopState(){
        return shopService.getShopState();
    }

}


